home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / wgdb-42.lha / wgdb-4.2 / gdb / command.c < prev    next >
C/C++ Source or Header  |  1992-09-11  |  32KB  |  1,213 lines

  1. /* Library for reading command lines and decoding commands.
  2.    Copyright (C) 1986, 1989, 1990 Free Software Foundation, Inc.
  3.  
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. GNU General Public License for more details.
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #include <stdio.h>
  19. #include "defs.h"
  20. #include "command.h"
  21. #include "symtab.h"
  22. #include "value.h"
  23. #include <ctype.h>
  24. #include <string.h>
  25.  
  26. extern char *getenv ();
  27.  
  28. /* Add element named NAME to command list *LIST.
  29.    FUN should be the function to execute the command;
  30.    it will get a character string as argument, with leading
  31.    and trailing blanks already eliminated.
  32.  
  33.    DOC is a documentation string for the command.
  34.    Its first line should be a complete sentence.
  35.    It should start with ? for a command that is an abbreviation
  36.    or with * for a command that most users don't need to know about.  */
  37.  
  38. struct cmd_list_element *
  39. add_cmd (name, class, fun, doc, list)
  40.      char *name;
  41.      enum command_class class;
  42.      void (*fun) ();
  43.      char *doc;
  44.      struct cmd_list_element **list;
  45. {
  46.   register struct cmd_list_element *c
  47.     = (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element));
  48.  
  49.   delete_cmd (name, list);
  50.   c->next = *list;
  51.   c->name = name;
  52.   c->class = class;
  53.   c->function = fun;
  54.   c->doc = doc;
  55.   c->prefixlist = 0;
  56.   c->prefixname = (char *)NULL;
  57.   c->allow_unknown = 0;
  58.   c->abbrev_flag = 0;
  59.   c->aux = 0;
  60.   c->type = not_set_cmd;
  61.   c->completer = make_symbol_completion_list;
  62.   c->var = 0;
  63.   c->var_type = var_boolean;
  64.   c->user_commands = 0;
  65.   *list = c;
  66.   return c;
  67. }
  68.  
  69. /* Same as above, except that the abbrev_flag is set. */
  70.  
  71. struct cmd_list_element *
  72. add_abbrev_cmd (name, class, fun, doc, list)
  73.      char *name;
  74.      enum command_class class;
  75.      void (*fun) ();
  76.      char *doc;
  77.      struct cmd_list_element **list;
  78. {
  79.   register struct cmd_list_element *c
  80.     = add_cmd (name, class, fun, doc, list);
  81.  
  82.   c->abbrev_flag = 1;
  83.   return c;
  84. }
  85.  
  86. struct cmd_list_element *
  87. add_alias_cmd (name, oldname, class, abbrev_flag, list)
  88.      char *name;
  89.      char *oldname;
  90.      enum command_class class;
  91.      int abbrev_flag;
  92.      struct cmd_list_element **list;
  93. {
  94.   /* Must do this since lookup_cmd tries to side-effect its first arg */
  95.   char *copied_name;
  96.   register struct cmd_list_element *old;
  97.   register struct cmd_list_element *c;
  98.   copied_name = (char *) alloca (strlen (oldname) + 1);
  99.   strcpy (copied_name, oldname);
  100.   old  = lookup_cmd (&copied_name, *list, "", 1, 1);
  101.  
  102.   if (old == 0)
  103.     {
  104.       delete_cmd (name, list);
  105.       return 0;
  106.     }
  107.  
  108.   c = add_cmd (name, class, old->function, old->doc, list);
  109.   c->prefixlist = old->prefixlist;
  110.   c->prefixname = old->prefixname;
  111.   c->allow_unknown = old->allow_unknown;
  112.   c->abbrev_flag = abbrev_flag;
  113.   c->aux = old->aux;
  114.   return c;
  115. }
  116.  
  117. /* Like add_cmd but adds an element for a command prefix:
  118.    a name that should be followed by a subcommand to be looked up
  119.    in another command list.  PREFIXLIST should be the address
  120.    of the variable containing that list.  */
  121.  
  122. struct cmd_list_element *
  123. add_prefix_cmd (name, class, fun, doc, prefixlist, prefixname,
  124.         allow_unknown, list)
  125.      char *name;
  126.      enum command_class class;
  127.      void (*fun) ();
  128.      char *doc;
  129.      struct cmd_list_element **prefixlist;
  130.      char *prefixname;
  131.      int allow_unknown;
  132.      struct cmd_list_element **list;
  133. {
  134.   register struct cmd_list_element *c = add_cmd (name, class, fun, doc, list);
  135.   c->prefixlist = prefixlist;
  136.   c->prefixname = prefixname;
  137.   c->allow_unknown = allow_unknown;
  138.   return c;
  139. }
  140.  
  141. /* Like add_prefix_cmd butsets the abbrev_flag on the new command. */
  142.    
  143. struct cmd_list_element *
  144. add_abbrev_prefix_cmd (name, class, fun, doc, prefixlist, prefixname,
  145.         allow_unknown, list)
  146.      char *name;
  147.      enum command_class class;
  148.      void (*fun) ();
  149.      char *doc;
  150.      struct cmd_list_element **prefixlist;
  151.      char *prefixname;
  152.      int allow_unknown;
  153.      struct cmd_list_element **list;
  154. {
  155.   register struct cmd_list_element *c = add_cmd (name, class, fun, doc, list);
  156.   c->prefixlist = prefixlist;
  157.   c->prefixname = prefixname;
  158.   c->allow_unknown = allow_unknown;
  159.   c->abbrev_flag = 1;
  160.   return c;
  161. }
  162.  
  163. /* ARGSUSED */
  164. void
  165. not_just_help_class_command (args, from_tty, c)
  166.      char *args;
  167.      int from_tty;
  168.      struct cmd_list_element *c;
  169. {
  170. }
  171.  
  172. /* Add element named NAME to command list LIST (the list for set
  173.    or some sublist thereof).
  174.    CLASS is as in add_cmd.
  175.    VAR_TYPE is the kind of thing we are setting.
  176.    VAR is address of the variable being controlled by this command.
  177.    DOC is the documentation string.  */
  178. struct cmd_list_element *
  179. add_set_cmd (name, class, var_type, var, doc, list)
  180.      char *name;
  181.      enum command_class class;
  182.      var_types var_type;
  183.      char *var;
  184.      char *doc;
  185.      struct cmd_list_element **list;
  186. {
  187.   /* For set/show, we have to call do_setshow_command
  188.      differently than an ordinary function (take commandlist as
  189.      well as arg), so the function field isn't helpful.  However,
  190.      function == NULL means that it's a help class, so set the function
  191.      to not_just_help_class_command.  */
  192.   struct cmd_list_element *c
  193.     = add_cmd (name, class, not_just_help_class_command, doc, list);
  194.  
  195.   c->type = set_cmd;
  196.   c->var_type = var_type;
  197.   c->var = var;
  198.   return c;
  199. }
  200.  
  201. /* Where SETCMD has already been added, add the corresponding show
  202.    command to LIST and return a pointer to it.  */
  203. struct cmd_list_element *
  204. add_show_from_set (setcmd, list)
  205.      struct cmd_list_element *setcmd;
  206.      struct cmd_list_element **list;
  207. {
  208.   struct cmd_list_element *showcmd =
  209.     (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element));
  210.  
  211.   bcopy (setcmd, showcmd, sizeof (struct cmd_list_element));
  212.   delete_cmd (showcmd->name, list);
  213.   showcmd->type = show_cmd;
  214.   
  215.   /* Replace "set " at start of docstring with "show ".  */
  216.   if (setcmd->doc[0] == 'S' && setcmd->doc[1] == 'e'
  217.       && setcmd->doc[2] == 't' && setcmd->doc[3] == ' ')
  218.     showcmd->doc = concat ("Show ", setcmd->doc + 4, "");
  219.   else
  220.     fprintf (stderr, "GDB internal error: Bad docstring for set command\n");
  221.   
  222.   showcmd->next = *list;
  223.   *list = showcmd;
  224.   return showcmd;
  225. }
  226.  
  227. /* Remove the command named NAME from the command list.  */
  228.  
  229. void
  230. delete_cmd (name, list)
  231.      char *name;
  232.      struct cmd_list_element **list;
  233. {
  234.   register struct cmd_list_element *c;
  235.   struct cmd_list_element *p;
  236.  
  237.   while (*list && !strcmp ((*list)->name, name))
  238.     {
  239.       p = (*list)->next;
  240.       free (*list);
  241.       *list = p;
  242.     }
  243.  
  244.   if (*list)
  245.     for (c = *list; c->next;)
  246.       {
  247.     if (!strcmp (c->next->name, name))
  248.       {
  249.         p = c->next->next;
  250.         free (c->next);
  251.         c->next = p;
  252.       }
  253.     else
  254.       c = c->next;
  255.       }
  256. }
  257.  
  258. /* This command really has to deal with two things:
  259.  *     1) I want documentation on *this string* (usually called by
  260.  * "help commandname").
  261.  *     2) I want documentation on *this list* (usually called by
  262.  * giving a command that requires subcommands.  Also called by saying
  263.  * just "help".)
  264.  *
  265.  *   I am going to split this into two seperate comamnds, help_cmd and
  266.  * help_list. 
  267.  */
  268.  
  269. void
  270. help_cmd (command, stream)
  271.      char *command;
  272.      FILE *stream;
  273. {
  274.   struct cmd_list_element *c;
  275.   extern struct cmd_list_element *cmdlist;
  276.  
  277.   if (!command)
  278.     {
  279.       help_list (cmdlist, "", all_classes, stream);
  280.       return;
  281.     }
  282.  
  283.   c = lookup_cmd (&command, cmdlist, "", 0, 0);
  284.  
  285.   if (c == 0)
  286.     return;
  287.  
  288.   /* There are three cases here.
  289.      If c->prefixlist is nonzero, we have a prefix command.
  290.      Print its documentation, then list its subcommands.
  291.      
  292.      If c->function is nonzero, we really have a command.
  293.      Print its documentation and return.
  294.      
  295.      If c->function is zero, we have a class name.
  296.      Print its documentation (as if it were a command)
  297.      and then set class to the number of this class
  298.      so that the commands in the class will be listed.  */
  299.  
  300.   fputs_filtered (c->doc, stream);
  301.   fputs_filtered ("\n", stream);
  302.  
  303.   if (c->prefixlist == 0 && c->function != 0)
  304.     return;
  305.   fprintf_filtered (stream, "\n");
  306.  
  307.   /* If this is a prefix command, print it's subcommands */
  308.   if (c->prefixlist)
  309.     help_list (*c->prefixlist, c->prefixname, all_commands, stream);
  310.  
  311.   /* If this is a class name, print all of the commands in the class */
  312.   if (c->function == 0)
  313.     help_list (cmdlist, "", c->class, stream);
  314. }
  315.  
  316. /*
  317.  * Get a specific kind of help on a command list.
  318.  *
  319.  * LIST is the list.
  320.  * CMDTYPE is the prefix to use in the title string.
  321.  * CLASS is the class with which to list the nodes of this list (see
  322.  * documentation for help_cmd_list below),  As usual, ALL_COMMANDS for
  323.  * everything, ALL_CLASSES for just classes, and non-negative for only things
  324.  * in a specific class.
  325.  * and STREAM is the output stream on which to print things.
  326.  * If you call this routine with a class >= 0, it recurses.
  327.  */
  328. void
  329. help_list (list, cmdtype, class, stream)
  330.      struct cmd_list_element *list;
  331.      char *cmdtype;
  332.      enum command_class class;
  333.      FILE *stream;
  334. {
  335.   int len;
  336.   char *cmdtype1, *cmdtype2;
  337.   
  338.   /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub"  */
  339.   len = strlen (cmdtype);
  340.   cmdtype1 = (char *) alloca (len + 1);
  341.   cmdtype1[0] = 0;
  342.   cmdtype2 = (char *) alloca (len + 4);
  343.   cmdtype2[0] = 0;
  344.   if (len)
  345.     {
  346.       cmdtype1[0] = ' ';
  347.       strncpy (cmdtype1 + 1, cmdtype, len - 1);
  348.       cmdtype1[len] = 0;
  349.       strncpy (cmdtype2, cmdtype, len - 1);
  350.       strcpy (cmdtype2 + len - 1, " sub");
  351.     }
  352.  
  353.   if (class == all_classes)
  354.     fprintf_filtered (stream, "List of classes of %scommands:\n\n", cmdtype2);
  355.   else
  356.     fprintf_filtered (stream, "List of %scommands:\n\n", cmdtype2);
  357.  
  358.   help_cmd_list (list, class, cmdtype, (int)class >= 0, stream);
  359.  
  360.   if (class == all_classes)
  361.     fprintf_filtered (stream, "\n\
  362. Type \"help%s\" followed by a class name for a list of commands in that class.",
  363.          cmdtype1);
  364.  
  365.   fprintf_filtered (stream, "\n\
  366. Type \"help%s\" followed by %scommand name for full documentation.\n\
  367. Command name abbreviations are allowed if unambiguous.\n",
  368.        cmdtype1, cmdtype2);
  369. }
  370.      
  371. /* Print only the first line of STR on STREAM.  */
  372. static void
  373. print_doc_line (stream, str)
  374.      FILE *stream;
  375.      char *str;
  376. {
  377.   static char *line_buffer = 0;
  378.   static int line_size;
  379.   register char *p;
  380.  
  381.   if (!line_buffer)
  382.     {
  383.       line_size = 80;
  384.       line_buffer = (char *) xmalloc (line_size);
  385.     }
  386.  
  387.   p = str;
  388.   while (*p && *p != '\n' && *p != '.' && *p != ',')
  389.     p++;
  390.   if (p - str > line_size - 1)
  391.     {
  392.       line_size = p - str + 1;
  393.       free (line_buffer);
  394.       line_buffer = (char *) xmalloc (line_size);
  395.     }
  396.   strncpy (line_buffer, str, p - str);
  397.   line_buffer[p - str] = '\0';
  398.   if (islower (line_buffer[0]))
  399.     line_buffer[0] = toupper (line_buffer[0]);
  400.   fputs_filtered (line_buffer, stream);
  401. }
  402.  
  403. /*
  404.  * Implement a help command on command list LIST.
  405.  * RECURSE should be non-zero if this should be done recursively on
  406.  * all sublists of LIST.
  407.  * PREFIX is the prefix to print before each command name.
  408.  * STREAM is the stream upon which the output should be written.
  409.  * CLASS should be:
  410.  *    A non-negative class number to list only commands in that
  411.  * class.
  412.  *    ALL_COMMANDS to list all commands in list.
  413.  *    ALL_CLASSES  to list all classes in list.
  414.  *
  415.  *   Note that RECURSE will be active on *all* sublists, not just the
  416.  * ones seclected by the criteria above (ie. the selection mechanism
  417.  * is at the low level, not the high-level).
  418.  */
  419. void
  420. help_cmd_list (list, class, prefix, recurse, stream)
  421.      struct cmd_list_element *list;
  422.      enum command_class class;
  423.      char *prefix;
  424.      int recurse;
  425.      FILE *stream;
  426. {
  427.   register struct cmd_list_element *c;
  428.  
  429.   for (c = list; c; c = c->next)
  430.     {
  431.       if (c->abbrev_flag == 0 &&
  432.       (class == all_commands
  433.       || (class == all_classes && c->function == 0)
  434.       || (class == c->class && c->function != 0)))
  435.     {
  436.       fprintf_filtered (stream, "%s%s -- ", prefix, c->name);
  437.       print_doc_line (stream, c->doc);
  438.       fputs_filtered ("\n", stream);
  439.     }
  440.       if (recurse
  441.       && c->prefixlist != 0
  442.       && c->abbrev_flag == 0)
  443.     help_cmd_list (*c->prefixlist, class, c->prefixname, 1, stream);
  444.     }
  445. }
  446.  
  447. /* This routine takes a line of TEXT and a CLIST in which to
  448.    start the lookup.  When it returns it will have incremented the text
  449.    pointer past the section of text it matched, set *RESULT_LIST to
  450.    the list in which the last word was matched, and will return the
  451.    cmd list element which the text matches.  It will return 0 if no
  452.    match at all was possible.  It will return -1 if ambigous matches are
  453.    possible; in this case *RESULT_LIST will be set to the list in which
  454.    there are ambiguous choices (and text will be set to the ambiguous
  455.    text string).
  456.  
  457.    It does no error reporting whatsoever; control will always return
  458.    to the superior routine.
  459.  
  460.    In the case of an ambiguous return (-1), *RESULT_LIST will be set to
  461.    point at the prefix_command (ie. the best match) *or* (special
  462.    case) will be 0 if no prefix command was ever found.  For example,
  463.    in the case of "info a", "info" matches without ambiguity, but "a"
  464.    could be "args" or "address", so *RESULT_LIST is set to
  465.    the cmd_list_element for "info".  So in this case
  466.    result list should not be interpeted as a pointer to the beginning
  467.    of a list; it simply points to a specific command.
  468.  
  469.    If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
  470.    affect the operation).
  471.  
  472.    This routine does *not* modify the text pointed to by TEXT.
  473.    
  474.    If IGNORE_HELP_CLASSES is nonzero, ignore any command list
  475.    elements which are actually help classes rather than commands (i.e.
  476.    the function field of the struct cmd_list_element is 0).  */
  477.  
  478. struct cmd_list_element *
  479. lookup_cmd_1 (text, clist, result_list, ignore_help_classes)
  480.      char **text;
  481.      struct cmd_list_element *clist, **result_list;
  482.      int ignore_help_classes;
  483. {
  484.   char *p, *command;
  485.   int len, tmp, nfound;
  486.   struct cmd_list_element *found, *c;
  487.  
  488.   while (**text == ' ' || **text == '\t')
  489.     (*text)++;
  490.  
  491.   /* Treating underscores as part of command words is important
  492.      so that "set args_foo()" doesn't get interpreted as
  493.      "set args _foo()".  */
  494.   for (p = *text;
  495.        *p && (isalnum(*p) || *p == '-' || *p == '_');
  496.        p++)
  497.     ;
  498.  
  499.   /* If nothing but whitespace, return 0.  */
  500.   if (p == *text)
  501.     return 0;
  502.   
  503.   len = p - *text;
  504.  
  505.   /* *text and p now bracket the first command word to lookup (and
  506.      it's length is len).  We copy this into a local temporary,
  507.      converting to lower case as we go.  */
  508.  
  509.   command = (char *) alloca (len + 1);
  510.   for (tmp = 0; tmp < len; tmp++)
  511.     {
  512.       char x = (*text)[tmp];
  513.       command[tmp] = (x >= 'A' && x <= 'Z') ? x - 'A' + 'a' : x;
  514.     }
  515.   command[len] = '\0';
  516.  
  517.   /* Look it up.  */
  518.   found = 0;
  519.   nfound = 0;
  520.   for (c = clist; c; c = c->next)
  521.     if (!strncmp (command, c->name, len)
  522.     && (!ignore_help_classes || c->function))
  523.       {
  524.     found = c;
  525.     nfound++;
  526.     if (c->name[len] == '\0')
  527.       {
  528.         nfound = 1;
  529.         break;
  530.       }
  531.       }
  532.  
  533.   /* If nothing matches, we have a simple failure.  */
  534.   if (nfound == 0)
  535.     return 0;
  536.  
  537.   if (nfound > 1)
  538.     {
  539.       if (result_list != NULL)
  540.     /* Will be modified in calling routine
  541.        if we know what the prefix command is.  */
  542.     *result_list = 0;        
  543.       return (struct cmd_list_element *) -1; /* Ambiguous.  */
  544.     }
  545.  
  546.   /* We've matched something on this list.  Move text pointer forward. */
  547.  
  548.   *text = p;
  549.   if (found->prefixlist)
  550.     {
  551.       c = lookup_cmd_1 (text, *found->prefixlist, result_list,
  552.             ignore_help_classes);
  553.       if (!c)
  554.     {
  555.       /* Didn't find anything; this is as far as we got.  */
  556.       if (result_list != NULL)
  557.         *result_list = clist;
  558.       return found;
  559.     }
  560.       else if (c == (struct cmd_list_element *) -1)
  561.     {
  562.       /* We've gotten this far properley, but the next step
  563.          is ambiguous.  We need to set the result list to the best
  564.          we've found (if an inferior hasn't already set it).  */
  565.       if (result_list != NULL)
  566.         if (!*result_list)
  567.           /* This used to say *result_list = *found->prefixlist
  568.          If that was correct, need to modify the documentation
  569.          at the top of this function to clarify what is supposed
  570.          to be going on.  */
  571.           *result_list = found;
  572.       return c;
  573.     }
  574.       else
  575.     {
  576.       /* We matched!  */
  577.       return c;
  578.     }
  579.     }
  580.   else
  581.     {
  582.       if (result_list != NULL)
  583.     *result_list = clist;
  584.       return found;
  585.     }
  586. }
  587.  
  588. /* All this hair to move the space to the front of cmdtype */
  589.  
  590. void
  591. undef_cmd_error (cmdtype, q)
  592.      char *cmdtype, *q;
  593. {
  594.   error ("Undefined %scommand: \"%s\".  Try \"help%s%.*s\".",
  595.     cmdtype,
  596.     q,
  597.     *cmdtype? " ": "",
  598.     strlen(cmdtype)-1,
  599.     cmdtype);
  600. }
  601.  
  602. /* Look up the contents of *LINE as a command in the command list LIST.
  603.    LIST is a chain of struct cmd_list_element's.
  604.    If it is found, return the struct cmd_list_element for that command
  605.    and update *LINE to point after the command name, at the first argument.
  606.    If not found, call error if ALLOW_UNKNOWN is zero
  607.    otherwise (or if error returns) return zero.
  608.    Call error if specified command is ambiguous,
  609.    unless ALLOW_UNKNOWN is negative.
  610.    CMDTYPE precedes the word "command" in the error message.
  611.  
  612.    If INGNORE_HELP_CLASSES is nonzero, ignore any command list
  613.    elements which are actually help classes rather than commands (i.e.
  614.    the function field of the struct cmd_list_element is 0).  */
  615.  
  616. struct cmd_list_element *
  617. lookup_cmd (line, list, cmdtype, allow_unknown, ignore_help_classes)
  618.      char **line;
  619.      struct cmd_list_element *list;
  620.      char *cmdtype;
  621.      int allow_unknown;
  622.      int ignore_help_classes;
  623. {
  624.   struct cmd_list_element *last_list = 0;
  625.   struct cmd_list_element *c =
  626.     lookup_cmd_1 (line, list, &last_list, ignore_help_classes);
  627.   char *ptr = (*line) + strlen (*line) - 1;
  628.  
  629.   /* Clear off trailing whitespace.  */
  630.   while (ptr >= *line && (*ptr == ' ' || *ptr == '\t'))
  631.     ptr--;
  632.   *(ptr + 1) = '\0';
  633.   
  634.   if (!c)
  635.     {
  636.       if (!allow_unknown)
  637.     {
  638.       if (!*line)
  639.         error ("Lack of needed %scommand", cmdtype);
  640.       else
  641.         {
  642.           char *p = *line, *q;
  643.  
  644.           while (isalnum(*p) || *p == '-')
  645.         p++;
  646.  
  647.           q = (char *) alloca (p - *line + 1);
  648.           strncpy (q, *line, p - *line);
  649.           q[p-*line] = '\0';
  650.           undef_cmd_error (cmdtype, q);
  651.         }
  652.     }
  653.       else
  654.     return 0;
  655.     }
  656.   else if (c == (struct cmd_list_element *) -1)
  657.     {
  658.       /* Ambigous.  Local values should be off prefixlist or called
  659.      values.  */
  660.       int local_allow_unknown = (last_list ? last_list->allow_unknown :
  661.                  allow_unknown);
  662.       char *local_cmdtype = last_list ? last_list->prefixname : cmdtype;
  663.       struct cmd_list_element *local_list =
  664.     (last_list ? *(last_list->prefixlist) : list);
  665.       
  666.       if (local_allow_unknown < 0)
  667.     {
  668.       if (last_list)
  669.         return last_list;    /* Found something.  */
  670.       else
  671.         return 0;        /* Found nothing.  */
  672.     }
  673.       else
  674.     {
  675.       /* Report as error.  */
  676.       int amb_len;
  677.       char ambbuf[100];
  678.  
  679.       for (amb_len = 0;
  680.            ((*line)[amb_len] && (*line)[amb_len] != ' '
  681.         && (*line)[amb_len] != '\t');
  682.            amb_len++)
  683.         ;
  684.       
  685.       ambbuf[0] = 0;
  686.       for (c = local_list; c; c = c->next)
  687.         if (!strncmp (*line, c->name, amb_len))
  688.           {
  689.         if (strlen (ambbuf) + strlen (c->name) + 6 < (int)sizeof ambbuf)
  690.           {
  691.             if (strlen (ambbuf))
  692.               strcat (ambbuf, ", ");
  693.             strcat (ambbuf, c->name);
  694.           }
  695.         else
  696.           {
  697.             strcat (ambbuf, "..");
  698.             break;
  699.           }
  700.           }
  701.       error ("Ambiguous %scommand \"%s\": %s.", local_cmdtype,
  702.          *line, ambbuf);
  703.       return 0;        /* lint */
  704.     }
  705.     }
  706.   else
  707.     {
  708.       /* We've got something.  It may still not be what the caller
  709.          wants (if this command *needs* a subcommand).  */
  710.       while (**line == ' ' || **line == '\t')
  711.     (*line)++;
  712.  
  713.       if (c->prefixlist && **line && !c->allow_unknown)
  714.     undef_cmd_error (c->prefixname, *line);
  715.  
  716.       /* Seems to be what he wants.  Return it.  */
  717.       return c;
  718.     }
  719.   return 0;
  720. }
  721.     
  722. #if 0
  723. /* Look up the contents of *LINE as a command in the command list LIST.
  724.    LIST is a chain of struct cmd_list_element's.
  725.    If it is found, return the struct cmd_list_element for that command
  726.    and update *LINE to point after the command name, at the first argument.
  727.    If not found, call error if ALLOW_UNKNOWN is zero
  728.    otherwise (or if error returns) return zero.
  729.    Call error if specified command is ambiguous,
  730.    unless ALLOW_UNKNOWN is negative.
  731.    CMDTYPE precedes the word "command" in the error message.  */
  732.  
  733. struct cmd_list_element *
  734. lookup_cmd (line, list, cmdtype, allow_unknown)
  735.      char **line;
  736.      struct cmd_list_element *list;
  737.      char *cmdtype;
  738.      int allow_unknown;
  739. {
  740.   register char *p;
  741.   register struct cmd_list_element *c, *found;
  742.   int nfound;
  743.   char ambbuf[100];
  744.   char *processed_cmd;
  745.   int i, cmd_len;
  746.  
  747.   /* Skip leading whitespace.  */
  748.  
  749.   while (**line == ' ' || **line == '\t')
  750.     (*line)++;
  751.  
  752.   /* Clear out trailing whitespace.  */
  753.  
  754.   p = *line + strlen (*line);
  755.   while (p != *line && (p[-1] == ' ' || p[-1] == '\t'))
  756.     p--;
  757.   *p = 0;
  758.  
  759.   /* Find end of command name.  */
  760.  
  761.   p = *line;
  762.   while (*p == '-'
  763.      || (*p >= 'a' && *p <= 'z')
  764.      || (*p >= 'A' && *p <= 'Z')
  765.      || (*p >= '0' && *p <= '9'))
  766.     p++;
  767.  
  768.   /* Look up the command name.
  769.      If exact match, keep that.
  770.      Otherwise, take command abbreviated, if unique.  Note that (in my
  771.      opinion) a null string does *not* indicate ambiguity; simply the
  772.      end of the argument.  */
  773.  
  774.   if (p == *line)
  775.     {
  776.       if (!allow_unknown)
  777.     error ("Lack of needed %scommand", cmdtype);
  778.       return 0;
  779.     }
  780.   
  781.   /* Copy over to a local buffer, converting to lowercase on the way.
  782.      This is in case the command being parsed is a subcommand which
  783.      doesn't match anything, and that's ok.  We want the original
  784.      untouched for the routine of the original command.  */
  785.   
  786.   processed_cmd = (char *) alloca (p - *line + 1);
  787.   for (cmd_len = 0; cmd_len < p - *line; cmd_len++)
  788.     {
  789.       char x = (*line)[cmd_len];
  790.       if (x >= 'A' && x <= 'Z')
  791.     processed_cmd[cmd_len] = x - 'A' + 'a';
  792.       else
  793.     processed_cmd[cmd_len] = x;
  794.     }
  795.   processed_cmd[cmd_len] = '\0';
  796.  
  797.   /* Check all possibilities in the current command list.  */
  798.   found = 0;
  799.   nfound = 0;
  800.   for (c = list; c; c = c->next)
  801.     {
  802.       if (!strncmp (processed_cmd, c->name, cmd_len))
  803.     {
  804.       found = c;
  805.       nfound++;
  806.       if (c->name[cmd_len] == 0)
  807.         {
  808.           nfound = 1;
  809.           break;
  810.         }
  811.     }
  812.     }
  813.  
  814.   /* Report error for undefined command name.  */
  815.  
  816.   if (nfound != 1)
  817.     {
  818.       if (nfound > 1 && allow_unknown >= 0)
  819.     {
  820.       ambbuf[0] = 0;
  821.       for (c = list; c; c = c->next)
  822.         if (!strncmp (processed_cmd, c->name, cmd_len))
  823.           {
  824.         if (strlen (ambbuf) + strlen (c->name) + 6 < sizeof ambbuf)
  825.           {
  826.             if (strlen (ambbuf))
  827.               strcat (ambbuf, ", ");
  828.             strcat (ambbuf, c->name);
  829.           }
  830.         else
  831.           {
  832.             strcat (ambbuf, "..");
  833.             break;
  834.           }
  835.           }
  836.       error ("Ambiguous %scommand \"%s\": %s.", cmdtype,
  837.          processed_cmd, ambbuf);
  838.     }
  839.       else if (!allow_unknown)
  840.     error ("Undefined %scommand: \"%s\".", cmdtype, processed_cmd);
  841.       return 0;
  842.     }
  843.  
  844.   /* Skip whitespace before the argument.  */
  845.  
  846.   while (*p == ' ' || *p == '\t') p++;
  847.   *line = p;
  848.  
  849.   if (found->prefixlist && *p)
  850.     {
  851.       c = lookup_cmd (line, *found->prefixlist, found->prefixname,
  852.               found->allow_unknown);
  853.       if (c)
  854.     return c;
  855.     }
  856.  
  857.   return found;
  858. }
  859. #endif
  860.  
  861. /* Helper function for SYMBOL_COMPLETION_FUNCTION.  */
  862.  
  863. /* Return a vector of char pointers which point to the different
  864.    possible completions in LIST of TEXT.  */
  865.  
  866. char **
  867. complete_on_cmdlist (list, text)
  868.      struct cmd_list_element *list;
  869.      char *text;
  870. {
  871.   struct cmd_list_element *ptr;
  872.   char **matchlist;
  873.   int sizeof_matchlist;
  874.   int matches;
  875.   int textlen = strlen (text);
  876.  
  877.   sizeof_matchlist = 10;
  878.   matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *));
  879.   matches = 0;
  880.  
  881.   for (ptr = list; ptr; ptr = ptr->next)
  882.     if (!strncmp (ptr->name, text, textlen)
  883.     && !ptr->abbrev_flag
  884.     && (ptr->function
  885.         || ptr->prefixlist))
  886.       {
  887.     if (matches == sizeof_matchlist)
  888.       {
  889.         sizeof_matchlist *= 2;
  890.         matchlist = (char **) xrealloc ((char *)matchlist,
  891.                         (sizeof_matchlist
  892.                          * sizeof (char *)));
  893.       }
  894.  
  895.     matchlist[matches] = (char *) 
  896.       xmalloc (strlen (ptr->name) + 1);
  897.     strcpy (matchlist[matches++], ptr->name);
  898.       }
  899.  
  900.   if (matches == 0)
  901.     {
  902.       free (matchlist);
  903.       matchlist = 0;
  904.     }
  905.   else
  906.     {
  907.       matchlist = (char **) xrealloc ((char *)matchlist, ((matches + 1)
  908.                         * sizeof (char *)));
  909.       matchlist[matches] = (char *) 0;
  910.     }
  911.  
  912.   return matchlist;
  913. }
  914.  
  915. static int
  916. parse_binary_operation (arg)
  917.      char *arg;
  918. {
  919.   int length;
  920.  
  921.   if (!arg || !*arg)
  922.     return 1;
  923.  
  924.   length = strlen (arg);
  925.  
  926.   while (arg[length - 1] == ' ' || arg[length - 1] == '\t')
  927.     length--;
  928.  
  929.   if (!strncmp (arg, "on", length)
  930.       || !strncmp (arg, "1", length)
  931.       || !strncmp (arg, "yes", length))
  932.     return 1;
  933.   else
  934.     if (!strncmp (arg, "off", length)
  935.     || !strncmp (arg, "0", length)
  936.     || !strncmp (arg, "no", length))
  937.       return 0;
  938.     else 
  939.       {
  940.     error ("\"on\" or \"off\" expected.");
  941.     return 0;
  942.       }
  943. }
  944.  
  945. /* Do a "set" or "show" command.  ARG is NULL if no argument, or the text
  946.    of the argument, and FROM_TTY is nonzero if this command is being entered
  947.    directly by the user (i.e. these are just like any other
  948.    command).  C is the command list element for the command.  */
  949. void
  950. do_setshow_command (arg, from_tty, c)
  951.      char *arg;
  952.      int from_tty;
  953.      struct cmd_list_element *c;
  954. {
  955.   if (c->type == set_cmd)
  956.     {
  957.       switch (c->var_type)
  958.     {
  959.     case var_string:
  960.       {
  961.         char *new;
  962.         char *p;
  963.         char *q;
  964.         int ch;
  965.         
  966.         if (arg == NULL)
  967.           arg = "";
  968.         new = (char *) xmalloc (strlen (arg) + 2);
  969.         p = arg; q = new;
  970.         while (ch = *p++)
  971.           {
  972.         if (ch == '\\')
  973.           {
  974.             /* \ at end of argument is used after spaces
  975.                so they won't be lost.  */
  976.             if (*p == 0)
  977.               break;
  978.             ch = parse_escape (&p);
  979.             if (ch == 0)
  980.               break; /* C loses */
  981.             else if (ch > 0)
  982.               *q++ = ch;
  983.           }
  984.         else
  985.           *q++ = ch;
  986.           }
  987.         if (*(p - 1) != '\\')
  988.           *q++ = ' ';
  989.         *q++ = '\0';
  990.         new = (char *) xrealloc (new, q - new);
  991.         if (*(char **)c->var != NULL)
  992.           free (*(char **)c->var);
  993.         *(char **) c->var = new;
  994.       }
  995.       break;
  996.     case var_string_noescape:
  997.       if (arg == NULL)
  998.         arg = "";
  999.       if (*(char **)c->var != NULL)
  1000.         free (*(char **)c->var);
  1001.       *(char **) c->var = savestring (arg, strlen (arg));
  1002.       break;
  1003.     case var_filename:
  1004.       if (arg == NULL)
  1005.         error_no_arg ("filename to set it to.");
  1006.       if (*(char **)c->var != NULL)
  1007.         free (*(char **)c->var);
  1008.       *(char **)c->var = tilde_expand (arg);
  1009.       break;
  1010.     case var_boolean:
  1011.       *(int *) c->var = parse_binary_operation (arg);
  1012.       break;
  1013.     case var_uinteger:
  1014.       if (arg == NULL)
  1015.         error_no_arg ("integer to set it to.");
  1016.       *(int *) c->var = parse_and_eval_address (arg);
  1017.       if (*(int *) c->var == 0)
  1018.         *(int *) c->var = UINT_MAX;
  1019.       break;
  1020.     case var_zinteger:
  1021.       if (arg == NULL)
  1022.         error_no_arg ("integer to set it to.");
  1023.       *(int *) c->var = parse_and_eval_address (arg);
  1024.       break;
  1025.     default:
  1026.       error ("gdb internal error: bad var_type in do_setshow_command");
  1027.     }
  1028.     }
  1029.   else if (c->type == show_cmd)
  1030.     {
  1031.       /* Print doc minus "show" at start.  */
  1032.       print_doc_line (stdout, c->doc + 5);
  1033.       
  1034.       fputs_filtered (" is ", stdout);
  1035.       wrap_here ("    ");
  1036.       switch (c->var_type)
  1037.     {
  1038.       case var_string:
  1039.     {
  1040.       unsigned char *p;
  1041.       fputs_filtered ("\"", stdout);
  1042.       for (p = *(unsigned char **) c->var; *p != '\0'; p++)
  1043.         printchar (*p, stdout, '"');
  1044.       fputs_filtered ("\"", stdout);
  1045.     }
  1046.     break;
  1047.       case var_string_noescape:
  1048.       case var_filename:
  1049.     fputs_filtered ("\"", stdout);
  1050.     fputs_filtered (*(char **) c->var, stdout);
  1051.     fputs_filtered ("\"", stdout);
  1052.     break;
  1053.       case var_boolean:
  1054.     fputs_filtered (*(int *) c->var ? "on" : "off", stdout);
  1055.     break;
  1056.       case var_uinteger:
  1057.     if (*(unsigned int *) c->var == UINT_MAX) {
  1058.       fputs_filtered ("unlimited", stdout);
  1059.       break;
  1060.     }
  1061.     /* else fall through */
  1062.       case var_zinteger:
  1063.     fprintf_filtered (stdout, "%d", *(unsigned int *) c->var);
  1064.     break;
  1065.       default:
  1066.     error ("gdb internal error: bad var_type in do_setshow_command");
  1067.       }
  1068.       fputs_filtered (".\n", stdout);
  1069.     }
  1070.   else
  1071.     error ("gdb internal error: bad cmd_type in do_setshow_command");
  1072.   (*c->function) (NULL, from_tty, c);
  1073. }
  1074.  
  1075. /* Show all the settings in a list of show commands.  */
  1076.  
  1077. void
  1078. cmd_show_list (list, from_tty, prefix)
  1079.      struct cmd_list_element *list;
  1080.      int from_tty;
  1081.      char *prefix;
  1082. {
  1083.   for (; list != NULL; list = list->next) {
  1084.     /* If we find a prefix, run its list, prefixing our output by its
  1085.        prefix (with "show " skipped).  */
  1086.     if (list->prefixlist && !list->abbrev_flag)
  1087.       cmd_show_list (*list->prefixlist, from_tty, list->prefixname + 5);
  1088.     if (list->type == show_cmd)
  1089.       {
  1090.     fputs_filtered (prefix, stdout);
  1091.     fputs_filtered (list->name, stdout);
  1092.     fputs_filtered (":  ", stdout);
  1093.     do_setshow_command ((char *)NULL, from_tty, list);
  1094.       }
  1095.   }
  1096. }
  1097.  
  1098. /* ARGSUSED */
  1099. static void
  1100. shell_escape (arg, from_tty)
  1101.      char *arg;
  1102.      int from_tty;
  1103. {
  1104.   int rc, status, pid;
  1105.   char *p, *user_shell;
  1106.   extern char *rindex ();
  1107.  
  1108.   if ((user_shell = (char *) getenv ("SHELL")) == NULL)
  1109.     user_shell = "/bin/sh";
  1110.  
  1111.   /* Get the name of the shell for arg0 */
  1112.   if ((p = rindex (user_shell, '/')) == NULL)
  1113.     p = user_shell;
  1114.   else
  1115.     p++;            /* Get past '/' */
  1116.  
  1117.   if ((pid = fork()) == 0)
  1118.     {
  1119.       if (!arg)
  1120.     execl (user_shell, p, 0);
  1121.       else
  1122.     execl (user_shell, p, "-c", arg, 0);
  1123.  
  1124.       fprintf (stderr, "Exec of shell failed\n");
  1125.       exit (0);
  1126.     }
  1127.  
  1128.   if (pid != -1)
  1129.     while ((rc = wait (&status)) != pid && rc != -1)
  1130.       ;
  1131.   else
  1132.     error ("Fork failed");
  1133. }
  1134.  
  1135. static void
  1136. make_command (arg, from_tty)
  1137.      char *arg;
  1138.      int from_tty;
  1139. {
  1140.   char *p;
  1141.  
  1142.   if (arg == 0)
  1143.     p = "make";
  1144.   else
  1145.     {
  1146.       p = xmalloc (sizeof("make ") + strlen(arg));
  1147.       strcpy (p, "make ");
  1148.       strcpy (p + sizeof("make ")-1, arg);
  1149.     }
  1150.   
  1151.   shell_escape (p, from_tty);
  1152. }
  1153.  
  1154. static void
  1155. user_info_1 (c, stream)
  1156.      struct cmd_list_element *c;
  1157.      FILE *stream;
  1158. {
  1159.   register struct command_line *cmdlines;
  1160.  
  1161.   cmdlines = c->user_commands;
  1162.   if (!cmdlines)
  1163.     return;
  1164.   fprintf_filtered (stream, "User command %s:\n", c->name);
  1165.   while (cmdlines)
  1166.     {
  1167.       fprintf_filtered (stream, "%s\n", cmdlines->line); 
  1168.       cmdlines = cmdlines->next;
  1169.     }
  1170.   fputs_filtered ("\n", stream);
  1171. }
  1172.  
  1173. /* ARGSUSED */
  1174. static void
  1175. user_info (args, from_tty)
  1176.      char *args;
  1177.      int from_tty;
  1178. {
  1179.   struct cmd_list_element *c;
  1180.   extern struct cmd_list_element *cmdlist;
  1181.  
  1182.   if (args)
  1183.     {
  1184.       c = lookup_cmd (&args, cmdlist, "", 0, 1);
  1185.       if (c->class != class_user)
  1186.     error ("Not a user command.");
  1187.       user_info_1 (c, stdout);
  1188.     }
  1189.   else
  1190.     {
  1191.       for (c = cmdlist; c; c = c->next)
  1192.     {
  1193.       if (c->class == class_user)
  1194.         user_info_1 (c, stdout);
  1195.     }
  1196.     }
  1197. }
  1198.  
  1199. void
  1200. _initialize_command ()
  1201. {
  1202.   add_com ("shell", class_support, shell_escape,
  1203.        "Execute the rest of the line as a shell command.  \n\
  1204. With no arguments, run an inferior shell.");
  1205.  
  1206.   add_com ("make", class_support, make_command,
  1207.        "Run the ``make'' program using the rest of the line as arguments.");
  1208.  
  1209.   add_info ("user", user_info, "Show definitions of user defined commands.\n\
  1210. Argument is the name of the user defined command.\n\
  1211. With no argument, show definitions of all user defined commands.");
  1212. }
  1213.